<template>
    <section>
        <!-- 添加弹出框 -->
        <el-dialog title="添加用户" :visible.sync="createVisible" :before-close="closeCreateDialog" width="40%">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="商户名称" prop="name">
                    <el-input v-model="form.name" ></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="form.mobile"></el-input>
                </el-form-item>
                <el-form-item label="登陆账号" prop="account">
                    <el-input v-model="form.account"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="repeat_password">
                    <el-input v-model="form.repeat_password" type="password"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeCreateDialog">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </el-dialog>
    </section>
</template>

<script>
    import { createMerchant } from '@/api/modules/merchant'

    export default {
        name: "UserCreateDialog",
        props: [
            'createVisible'
        ],
        watch: {
           
        },
        data() {
            return {
                roles: {},
                form: {
                    status: 1,
                    account: '',
                    password: '',
                    repeat_password: '',
                    name: '',
                    mobile: ''
                },
                rules: {
                    account: [
                        { required: true, message: '请输入登陆账号', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '密码不能为空', trigger: 'blur' },
                        { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
                    ],
                    repeat_password: [
                        { required: true, message: '确认密码不能为空', trigger: 'blur' },
                        { validator: this.checkPass, trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输入商户名称', trigger: 'blur' }
                    ],
                    mobile: [
                        {
                            pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/,
                            message: '手机号格式不对',
                            trigger: 'blur'
                        }
                    ]
                }
            }
        },
        methods:{
            closeCreateDialog(){
              this.$emit('closeCreateDialog')
            },
            submitForm(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        createMerchant({...this.form}).then(res => {
                            if(res.status === true){
                                this.$message.success({
                                    message: '操作成功',
                                    type: 'success',
                                })
                                this.closeCreateDialog();
                                this.$emit('refreshList')
                            }else{
                                this.$message.error({
                                    message: res.message,
                                    type: 'error',
                                })
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            checkPass(rule, value, callback) {
                if (value === '') {
                    callback(new Error('请输入确认密码'));
                } else {
                    if (this.form.repeat_password !== this.form.password) {
                        callback(new Error('两次密码不一致'));
                    }
                    callback();
                }
            }
        }
    }
</script>

<style scoped>

</style>